package first.program;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;



public class LoseScreen extends Activity implements OnClickListener {
	public static final String PREFS_NAME = "MyPrefsFile"; // Shared Preference File
	private static final int feedbackConstant = 3;
	final CreateGame game = new CreateGame();
	
	String [] pw;
	String [] passcode = new String[4];
	String colors[] = {"green", "orange", "black", "blue","red", "purple","yellow","gray"};
	Button temp = null;
	int mode;
	boolean rand_setting;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.losescreen);
    	Bundle bundle = getIntent().getExtras();{
    		try{
    			mode = bundle.getInt("mode"); 
    			pw = bundle.getStringArray("passcode");
    		}catch(Exception e1){Log.i("Error at bundle ", e1.toString());}}
    	
    	for(int i = 0; i < 4; i++){
    	  String ButtonID ="p"+(i+1);
    	  String color = "circle_"+pw[i];
    	  Log.i("cs", "this is iteration number "+i);
   		  int resID = getResources().getIdentifier(ButtonID, "id", "first.program");
    	  int resID2 = getResources().getIdentifier(color, "drawable", "first.program");
    	  temp = ((Button) findViewById(resID));
    	  Log.i("cs", "this is iteration number "+i);
    	  temp.setBackgroundResource(resID2);
    	 }
    	  
    	
    	
    	
    	
    	  Button play_again = (Button) findViewById(R.id.playagain);
    	  play_again.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
            	  view.performHapticFeedback(feedbackConstant , 0);
            	 
            	  
                final SharedPreferences settings= getSharedPreferences(PREFS_NAME, 0);
                rand_setting = settings.getBoolean("rand", false);  
            	if(mode == 1){ 
                		
                		if(rand_setting){
                      	Log.i("LoseScreen", "Repeats are allowed");
                      	game.createRep();
                      	passcode = game.getPassword();          	
                      	}
                      else{
                      		Log.i("LoseScreen", "Repeats not allowed");
                      		game.createNonRep();
                          	passcode = game.getPassword();

                      	}
                      		
                          Intent myIntent = new Intent(view.getContext(), GameScreen.class);
                          myIntent.putExtra("passcode", passcode);
                          myIntent.putExtra("mode", 1);
                          startActivityForResult(myIntent, 0);
                      }         		
            	else if(mode == 0){
            		Intent myIntent = new Intent(view.getContext(), CodeScreen.class);
            		startActivityForResult(myIntent, 0);
            		
            	}
                  
              }

          });
    	  
    	  
    	  
    	  
    	  
    	  
    	  Button home_again = (Button) findViewById(R.id.home);
    	 home_again.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
            	  	view.performHapticFeedback(feedbackConstant , 0);
            		Intent myIntent = new Intent(view.getContext(), MasterMindActivity.class);
            		startActivityForResult(myIntent, 0);
            		
      
                  
              }

          });
    	  
    	 
    	}
    

	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onBackPressed(){
	return;	
	}
}
    
    
  
  